/////precompiled////////
#include "precompiled.h"
////////////////////////


/**********
#include <typeinfo.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <stdlib.h>
#include <string.h>                      
#include <ctype.h>
//#include "igor\TInfoWindow.h"
****************/

#include "svl_TNodeS.h"
using namespace svl;
                      
// TNodeSArray //////////////////////////////////////////////////////////////////
//svl_CppMap(TNodeSArray);

//                            
void TNodeSArray::AddFirst(TNodeS *AddNode,bool EnDelete)
{
  AddNode->Parent = Parent; TArray::InsertUp(0,AddNode,EnDelete);
}

//
void TNodeSArray::AddLast(TNodeS *AddNode,bool EnDelete)
{
  AddNode->Parent = Parent; TArray::Add(AddNode,EnDelete);
}

//
void TNodeSArray::AddUp(TNodeS *CurrNode,TNodeS *AddNode,bool EnDelete)
{
  AddNode->Parent = Parent; TArray::InsertUp(Get(CurrNode),AddNode,EnDelete);
}

//
void TNodeSArray::AddDown(TNodeS *CurrNode,TNodeS *AddNode,bool EnDelete)
{
  AddNode->Parent = Parent; TArray::InsertDn(Get(CurrNode),AddNode,EnDelete);
}

//
TNodeS *TNodeSArray::NewByNamePath(TString SPath)
{
  char *S = SPath;
  int   L = lstrlen(S);
  int   Pos = 0;

  TString SName; for (int N = 0; N < L; N++)
  {
    if (S[N] == '.')
    {
      if (N - Pos > 0) // node
      {
        SPath.DelSubStr(Pos,N-Pos+1,SName);
        SName.DelLastSymbolIfExists('.');

        TNodeS *NewNode = new TNodeS(SName); AddLast(NewNode);
        NewNode->Children.NewByNamePath(SPath);
        return NewNode;
      }
      Pos = N + 1;
    }
    else if (N + 1 == L) // leaf
    {
      TNodeS *NewNode = new TNodeS(SPath); AddLast(NewNode);
      return NewNode;
    }
  }
  return NULL;
}

//
TNodeS *TNodeSArray::NewLast() 
{ 
  TNodeS *P = new TNodeS; AddLast(P,true); return P;
}

//
void TNodeSArray::NewFrom(TNodeSArray &InNodes)
{
  for (int N = 0; N < InNodes.Count(); N++)
  {
    TNodeS *NewNode = new TNodeS;
      NewNode->Name  = InNodes[N].Name;
      NewNode->Value = InNodes[N].Value;
    AddLast(NewNode,true);

    NewNode->Children.NewFrom(InNodes[N].Children);
  }
}

//
TNodeS *TNodeSArray::New(char *Name) 
{ 
  TNodeS *P = NewLast(); P->Name = Name; return P;
}

//
void TNodeSArray::Delete(TNodeS *DelNode)
{
  TArray::Delete(DelNode);
}

//
int TNodeSArray::Get(TNodeS *Node) 
{ 
  return TArray::ObjToN(Node);
}

//
TNodeS *TNodeSArray::SearchByName(TString Name)
{
  for (int N = 0; N < Count(); N++)
  {
    if (Get(N).Name.CompareIC(Name))
    {
      return &Get(N);
    }
  }
  return NULL;
}

//
void TNodeSArray::WriteTo(TString &To,TString SOffs)
{
  for (int N = 0; N < Count(); N++)
  {
    Get(N).WriteTo(To,SOffs);
  }
}

// TNodeS ///////////////////////////////////////////////////////////////////////
//svl_CppMap(TNodeS);

//
TNodeS::TNodeS() : Children(this)
{
  Parent = NULL;
}

//
TNodeS::TNodeS(char *Name) : Children(this)
{
  Parent = NULL; this->Name = Name;
}

//
TNodeS *TNodeS::New(char *Name)
{
  TNodeS *P = new TNodeS(Name); Children.AddLast(P); return P;
}

//
TString TNodeS::GetAsString(TString SOffs)
{
  TString S(SOffs); S << Name << " "; if (!Value.IsNull())
  {
    S << "= " << Value.GetInfo();
  }
  S << "\r\n";

  if (Children.Count())
  {
    S << SOffs << "{\r\n"; for (int N = 0; N < Children.Count(); N++)
    {
      S << Children[N].GetAsString(SOffs + "  ");
    }
    S << SOffs << "}\r\n";
  }
  return S;
}

//
void TNodeS::BuildTo(TMem &Mem)
{
  Mem.Clear(); TString S = GetAsString(); Mem.Add((BYTE*)S.S(),S.Len());
}

//
void TNodeS::WriteTo(TString &To,TString SOffs)
{
  To << SOffs << Name << " "; if (!Value.IsNull())
  {
    To << "= " << Value.GetInfo();
  }
  To << "\r\n";

  if (Children.Count())
  {
    To << SOffs << "{\r\n"; for (int N = 0; N < Children.Count(); N++)
    {
      Children[N].WriteTo(To,SOffs + "  ");
    }
    To << SOffs << "}\r\n";
  }
}

